﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Expressions;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;
using System.Web.UI;

namespace NotAClue.Web.DynamicData
{
    public static class QueryExtenderExtensionMethods
    {
        /// <summary>
        /// Sets the initial sort order.
        /// </summary>
        /// <param name="queryExtender">The query extender.</param>
        /// <param name="table">The table.</param>
        public static void SetInitialSortOrder(this QueryExtender queryExtender, MetaTable table)
        {
            // set default sort
            if (table.SortColumn != null)
            {
                var order = new OrderByExpression()
                {
                    DataField = table.SortColumn.Name,
                    Direction = table.SortDescending ? SortDirection.Descending : SortDirection.Ascending,
                };
                queryExtender.Expressions.Add(order);
            }
        }

        /// <summary>
        /// Sets up the multi column text search.
        /// </summary>
        /// <param name="queryExtender">The query extender.</param>
        /// <param name="table">The table.</param>
        /// <param name="dataSource">The data source.</param>
        /// <param name="context">The context.</param>
        /// <param name="searchTextbox.Id">The text box id.</param>
        /// <returns></returns>
        public static Boolean SetupMultiColumnTextSearch(this QueryExtender queryExtender, MetaTable table, IQueryableDataSource dataSource, HttpContext context, TextBox searchTextbox)
        {
            // get multi column search attribute
            var multiColumnSearch = table.GetAttribute<MultiColumnSearchAttribute>();

            if (multiColumnSearch != null)
            {
                String dataFields = String.Empty;
                // get list of columns from attribute else get all string columns.
                if (multiColumnSearch.Columns.Length > 0)
                    dataFields = multiColumnSearch.Columns.ToCsvString();
                else
                    dataFields = table.GetSearchableColumnNames().ToCsvString();

                var searchExpression = new SearchExpression()
                {
                    DataFields = dataFields,
                    SearchType = SearchType.Contains
                };

                // set text box width
                if (multiColumnSearch.Width > 0)
                    searchTextbox.Width = multiColumnSearch.Width;

                // create control parameter
                var controlParameter = new ControlParameter() { ControlID = searchTextbox.ID };

                // add control parameter to search expression
                searchExpression.Parameters.Add(controlParameter);

                // set search expression context
                searchExpression.SetContext(queryExtender, context, dataSource);

                // add search expression to query extender
                queryExtender.Expressions.Add(searchExpression);

                return true;
            }
            return false;
        }
    }
}
